home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / score / score1.bas < prev    next >
BASIC Source File  |  1995-06-17  |  7KB  |  207 lines

  1. Option Explicit
  2. '------------------------------------------------------------
  3. ' SCORE1.BAS
  4. '------------------------------------------------------------
  5.  
  6. ' Constant used to display the form modally.
  7. Const MODAL = 1
  8.  
  9. ' The maximum number of High Scores tracked.
  10. Const MAX_HISCORES = 5
  11.  
  12. ' Used when calling the two API functions below.
  13. Const SECTION = "HiScores"
  14. Const ENTRY = "Score"
  15.  
  16. ' Two Windows API calls used to read and write private .INI files.
  17. Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  18. Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
  19.  
  20. ' The data structure that stores high scores.
  21. Type tScores
  22.     Name As String
  23.     Score As Long
  24. End Type
  25.  
  26. ' This should ALWAYS be set to MAX_HISCORES + 1.
  27. Global Hi(1 To 6) As tScores
  28.  
  29. ' The current number of high scores stored in Hi() array.
  30. Global Num_HiScores As Integer
  31.  
  32. ' The new score to test.
  33. Global gNewScore As Long
  34.  
  35. ' The .INI file to store scoring information.
  36. Global gINIFile As String
  37.  
  38. ' This global boolean tells the form to just display
  39. ' the high scores (no player name data entry).
  40. Global gDisplayOnly As Integer
  41.  
  42. ' The game title displayed as the form caption
  43. ' of the High Score window.
  44. Global gGameTitle As String
  45.  
  46. Sub AddScoreAndSave (ByVal NewName As String, ByVal NewScore As Long)
  47. '------------------------------------------------------------
  48. ' Add this new score to the list of high scores and save
  49. ' everything back to the .INI file.
  50. '------------------------------------------------------------
  51. Dim i As Integer
  52. Dim j As Integer
  53. Dim temp As tScores
  54.  
  55.     ' Add the new score to the end of the Hi() array.
  56.     Hi(Num_HiScores + 1).Name = NewName
  57.     Hi(Num_HiScores + 1).Score = NewScore
  58.  
  59.     ' Bubble-sort the scores in descending order (highest first) ...
  60.     For j = 1 To Num_HiScores + 1
  61.         For i = 2 To Num_HiScores + 1
  62.             If Hi(i).Score > Hi(i - 1).Score Then
  63.                 temp = Hi(i - 1)
  64.                 Hi(i - 1) = Hi(i)
  65.                 Hi(i) = temp
  66.             End If
  67.         Next
  68.     Next
  69.  
  70.     If Num_HiScores < MAX_HISCORES Then Num_HiScores = Num_HiScores + 1
  71.  
  72.     ' Write the scores back to the .INI file.
  73.     For i = 1 To Num_HiScores
  74.         WriteScore gINIFile, i, Format$(Hi(i).Score) & ";" & Trim$(Hi(i).Name)
  75.     Next
  76.  
  77. End Sub
  78.  
  79. Sub GetScores (INIFile As String)
  80. '------------------------------------------------------------
  81. ' Read scores from the .INI file and store them in the Hi()
  82. ' array.  In the .INI file, the score and player's name are
  83. ' stored together, separated by a semi-colon like this:
  84. '
  85. ' ENTRY1=9999;name
  86. '
  87. ' We separate the two pieces of data after reading them in.
  88. '------------------------------------------------------------
  89. Dim i As Integer
  90. Dim rc As Integer
  91. Dim pos As Integer
  92. Dim AString As String
  93. Dim DefValue As String
  94.  
  95.  
  96.     For i = 1 To MAX_HISCORES
  97.         AString = Space$(255)
  98.  
  99.         ' Windows API call to retrieve data from an .INI file.
  100.         rc = GetPrivateProfileString(SECTION, ENTRY & Format$(i), "", AString, 255, INIFile)
  101.  
  102.         If rc > 0 Then
  103.             ' rc tells us the length of the returned string,
  104.             ' so we truncate the string at that length.
  105.             AString = Left$(AString, rc)
  106.  
  107.             ' Separate the player's name and score.
  108.             pos = InStr(AString, ";")
  109.             If pos > 0 Then
  110.                 Hi(i).Score = Left(AString, pos - 1)
  111.                 Hi(i).Name = Mid$(AString, pos + 1)
  112.             End If
  113.         Else
  114.             Num_HiScores = i - 1
  115.             Exit Sub
  116.         End If
  117.     Next
  118.     Num_HiScores = MAX_HISCORES
  119. End Sub
  120.  
  121. Function IsAHiScore (NewScore As Long) As Integer
  122. '------------------------------------------------------------
  123. ' Returns True if NewScore is a High Score, False otherwise.
  124. '------------------------------------------------------------
  125. Dim i As Integer
  126.  
  127.     ' Assume that it's not a high score.
  128.     IsAHiScore = False
  129.     
  130.     If Num_HiScores > 0 Then
  131.         ' If we've only equalled the lowest high score,
  132.         ' then don't bother...
  133.         If Num_HiScores = MAX_HISCORES And (NewScore = Hi(Num_HiScores).Score) Then
  134.             Exit Function
  135.         End If
  136.     End If
  137.  
  138.     ' If we haven't filled up the High Scores table,
  139.     ' then this must be a new high score.
  140.     If Num_HiScores < MAX_HISCORES Then
  141.         IsAHiScore = True
  142.         Exit Function
  143.     End If
  144.  
  145.     ' Compare this new score to the existing high scores.
  146.     For i = 1 To Num_HiScores
  147.         If Hi(i).Score <= NewScore Then
  148.             IsAHiScore = True
  149.             Exit For
  150.         End If
  151.     Next
  152.  
  153. End Function
  154.  
  155. Sub Main ()
  156. '------------------------------------------------------------
  157. ' This test procedure should be removed when using this
  158. ' module in your programs.  It's included to show the proper
  159. ' way to call the ShowHiScores routine.
  160. '------------------------------------------------------------
  161. Dim Score As Long
  162.  
  163.     On Error Resume Next
  164.     Score = CLng(Command$)
  165.  
  166.     ShowHiScores Score, "My Game", "MYAPP2.INI", True
  167.     End
  168. End Sub
  169.  
  170. Sub ShowHiScores (ByVal NewScore As Long, ByVal GameTitle As String, ByVal INIFile As String, ByVal DisplayOnly As Integer)
  171. '------------------------------------------------------------
  172. ' Call this routine after a game is completed, passing it
  173. ' the score of the game and the .INI file where your game's
  174. ' high scores are stored.  If the new score is a new high,
  175. ' then a form is displayed where the player can enter their
  176. ' name.  This information is then stored back in the .INI file.
  177. '------------------------------------------------------------
  178.  
  179.     gINIFile = INIFile
  180.     gGameTitle = GameTitle
  181.  
  182.     ' Get the current high scores from the .INI file.
  183.     GetScores gINIFile
  184.  
  185.     If DisplayOnly Then
  186.         gDisplayOnly = True
  187.         frmScores.Show MODAL
  188.     Else
  189.         ' If this is a new high, then display the High Score form.
  190.         If IsAHiScore(NewScore) Then
  191.             gNewScore = NewScore
  192.             frmScores.Show MODAL
  193.         End If
  194.     End If
  195. End Sub
  196.  
  197. Sub WriteScore (ByVal FileName As String, EntryNum As Integer, ByVal AString As String)
  198. '------------------------------------------------------------
  199. ' Write a high score back to the .INI file.
  200. '------------------------------------------------------------
  201. Dim rc As Integer
  202.  
  203.     ' Windows API call to write to a private .INI file.
  204.     rc = WritePrivateProfileString(SECTION, ENTRY & Format$(EntryNum), AString, FileName)
  205. End Sub
  206.  
  207.